home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / Bounce / Bounce.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.2 KB  |  126 lines

  1. /*---------------------------------------
  2.    BOUNCE.C -- Palette Animation Demo
  3.                (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER 1
  9.  
  10. TCHAR szAppName [] = TEXT ("Bounce") ;
  11. TCHAR szTitle   [] = TEXT ("Bounce: Palette Animation Demo") ;
  12.  
  13. static LOGPALETTE * plp ;
  14.  
  15. HPALETTE CreateRoutine (HWND hwnd)
  16. {
  17.      HPALETTE hPalette ;
  18.      int      i ;
  19.  
  20.      plp = malloc (sizeof (LOGPALETTE) + 33 * sizeof (PALETTEENTRY)) ;
  21.  
  22.      plp->palVersion    = 0x0300 ;
  23.      plp->palNumEntries = 34 ;
  24.  
  25.      for (i = 0 ; i < 34 ; i++)
  26.      {
  27.           plp->palPalEntry[i].peRed   = 255 ;
  28.           plp->palPalEntry[i].peGreen = (i == 0 ? 0 : 255) ;
  29.           plp->palPalEntry[i].peBlue  = (i == 0 ? 0 : 255) ;
  30.           plp->palPalEntry[i].peFlags = (i == 33 ? 0 : PC_RESERVED) ;
  31.      }
  32.      hPalette = CreatePalette (plp) ;
  33.  
  34.      SetTimer (hwnd, ID_TIMER, 50, NULL) ;
  35.      return hPalette ;
  36. }
  37.  
  38. void PaintRoutine (HDC hdc, int cxClient, int cyClient)
  39. {
  40.      HBRUSH hBrush ;
  41.      int    i, x1, x2, y1, y2 ;
  42.      RECT   rect ;
  43.  
  44.           // Draw window background using palette index 33
  45.  
  46.      SetRect (&rect, 0, 0, cxClient, cyClient) ;
  47.      hBrush = CreateSolidBrush (PALETTEINDEX (33)) ;
  48.      FillRect (hdc, &rect, hBrush) ;
  49.      DeleteObject (hBrush) ;
  50.  
  51.           // Draw the 33 balls
  52.  
  53.      SelectObject (hdc, GetStockObject (NULL_PEN)) ;
  54.  
  55.      for (i = 0 ; i < 33 ; i++)
  56.      {
  57.           x1 =  i      * cxClient / 33 ;
  58.           x2 = (i + 1) * cxClient / 33 ;
  59.  
  60.           if (i < 9)
  61.           {
  62.                y1  = i      * cyClient / 9 ;
  63.                y2 = (i + 1) * cyClient / 9 ;
  64.           }
  65.           else if (i < 17)
  66.           {
  67.                y1 = (16 - i) * cyClient / 9 ;
  68.                y2 = (17 - i) * cyClient / 9 ;
  69.           }
  70.           else if (i < 25)
  71.           {
  72.                y1 = (i - 16) * cyClient / 9 ;
  73.                y2 = (i - 15) * cyClient / 9 ;
  74.           }
  75.           else 
  76.           {
  77.                y1 = (32 - i) * cyClient / 9 ;
  78.                y2 = (33 - i) * cyClient / 9 ;
  79.           }
  80.  
  81.           hBrush = CreateSolidBrush (PALETTEINDEX (i)) ;
  82.           SelectObject (hdc, hBrush) ;
  83.           Ellipse (hdc, x1, y1, x2, y2) ;
  84.           DeleteObject (SelectObject (hdc, GetStockObject (WHITE_BRUSH))) ;
  85.      }
  86.      return ;
  87. }
  88.  
  89. void TimerRoutine (HDC hdc, HPALETTE hPalette)
  90. {
  91.      static BOOL bLeftToRight = TRUE ;
  92.      static int  iBall ;
  93.  
  94.           // Set old ball to white
  95.  
  96.      plp->palPalEntry[iBall].peGreen = 255 ;
  97.      plp->palPalEntry[iBall].peBlue  = 255 ;
  98.  
  99.      iBall += (bLeftToRight ? 1 : -1) ;
  100.  
  101.      if (iBall == (bLeftToRight ? 33 : -1))
  102.      {
  103.           iBall = (bLeftToRight ? 31 : 1) ;
  104.           bLeftToRight ^= TRUE ;
  105.      }
  106.  
  107.           // Set new ball to red
  108.  
  109.      plp->palPalEntry[iBall].peGreen = 0 ;
  110.      plp->palPalEntry[iBall].peBlue  = 0 ;
  111.  
  112.           // Animate the palette
  113.  
  114.      AnimatePalette (hPalette, 0, 33, plp->palPalEntry) ;
  115.      return ;
  116. }
  117.  
  118. void DestroyRoutine (HWND hwnd, HPALETTE hPalette)
  119. {
  120.      KillTimer (hwnd, ID_TIMER) ;
  121.      DeleteObject (hPalette) ;
  122.      free (plp) ;
  123.      return ;
  124. }
  125.  
  126.